<template>
  <div>
    <div class="gs-menu-bg"></div>
    <div class="gs-header">
      <div class="gs-quick-menu">
        <div class="gs-profile">
          <span>你好</span>&nbsp;&nbsp;&nbsp;&nbsp;
          <router-link class="gs-link" to='/userinfo'>
            <span v-if="user!==''" style="color: red">{{user.name}}</span>
            <span v-if="user===''" style="color: red">请登录</span>
          </router-link>
          <div v-if="user!==''" class="gs-profile" style="margin-left: 5px"><span>消息<strong>0</strong></span></div>
        </div>
        <div class="gs-profile" v-if="user!==''" style="margin-left: 10px">
          <router-link class="gs-link" to='/addStore'>
            <span>我要开店</span>
          </router-link>
        </div>
        <div class="gs-profile" v-if="user!==''" style="margin-left: 10px" @click="signOut">
          <router-link class="gs-link" to=''>
            <span>退出登录</span>
          </router-link>
        </div>
        <ul class="gs-nav">
          <li class="gs-nav-item">
            <router-link class="gs-link" to="/order">
              <i class="el-icon-s-home"/>我的订单</router-link>
          </li>
          <li class="gs-nav-item">
            <router-link class="gs-link" to="/cart">
              <i class="fa fa-shopping-cart"/>&nbsp;购物车
            </router-link>
          </li>
          <li class="gs-nav-item">
            <router-link class="gs-link " to="/collection">
              <i class="el-icon-s-comment"/>评论管理
				    </router-link>
          </li>
<!--          <li class="gs-nav-item">
            <router-link class="gs-link " to="/merchants">
				    多多客服
				    </router-link>
          </li>-->
          <li class="gs-nav-item">
            <router-link class="gs-link " to="">
              <a @click="store">商家管理员入口</a>
            </router-link>
          </li>
        </ul>
      </div>
      <div>
        <div class="gs-logo" @click="toHomepage"></div>
        <div class="gs-search-box">
          <input
            class="gs-search-input"
            type="text"
            placeholder="请输入 商品"
            v-model="search_word"
            >
            <span class="gs-search-btn" @click="searchGoods" >
            搜&nbsp;索
            </span>
        </div>
      </div>
    </div>
    <div class="gs-dividor"></div>
    <router-view/>
    <Foot></Foot>
  </div>

</template>

<script>
import UserInfo from'@/components/UserInfo'
import Search from'./components/Search.vue'
import Foot from'./views/Foot'
import {getToken,removeToken} from "@/util/auth";
  export default {
    name: 'App',
    data(){
      return{
        search_word:"",
        user:'',
        status:'可连接'
      }
    },
    mounted() {
      this.localSocket()
    },
    watch:{
      $route(to, from) {
        if(getToken()===undefined){
           return
         }
        this.localSocket()
        }
      },
    components:{
      Search,UserInfo,Foot
    },
    methods: {
      //退出登录
      signOut(done){
          this.$confirm('确认退出？')
                  .then(_ => {
                    this.closeWebSocket ();
                    removeToken();
                    location.reload();
                    done();
                  })
                  .catch(_ => {});
      },
      //webSocket 连接
      localSocket(){
        if ('WebSocket' in window) {
          if (this.status==='可连接'){
            if (getToken()!==undefined){
              this.user = JSON.parse(getToken())
              this.websocket = new WebSocket('ws://localhost:7777/websocket/'
                      + this.user.id);
              this.$global.setWs(this.websocket)
              this.initWebSocket()
              this.status='已连接'
            }
          }
        } else {
          alert('当前浏览器不支持!')
        }

      },

      initWebSocket () {
        // 连接错误
        this.$global.ws.onerror = this.setErrorMessage
        // 连接成功
        this.$global.ws.onopen = this.setOnopenMessage

        // 收到消息的回调
        this.$global.ws.onmessage = this.setOnmessageMessage

        // 连接关闭的回调
        this.$global.ws.onclose = this.setOncloseMessage

        // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = this.onbeforeunload
      },
      setErrorMessage () {
        console.log('WebSocket连接发生错误 状态码：' + this.$global.ws.readyState)
      },
      setOnopenMessage () {
        console.log(this.user.name+'WebSocket连接成功    状态码：' + this.$global.ws.readyState)
      },
      setOnmessageMessage (event) {
        const datas = JSON.parse(event.data);
        // 根据服务器推送的消息做自己的业务处理
        if (datas.type==='订单推送'){
          //console.log('服务端返回：' + datas)
          this.$notify.success({
            title: '提示',
            message: datas.msg,
            duration: 0,
            offset: 100,
            onClose:()=>{
              const id = this.user.id;
              this.$http({
                method:"get",
                url:"/api/pxx-websocket/removeWebSocket",
                params: {
                  id: id
                }
              })
            }
          });
      }
      },
      setOncloseMessage () {
        this.status='可连接'
        console.log(this.user.name+'WebSocket连接关闭    状态码：' + this.$global.ws.readyState)
      },
      closeWebSocket () {
        this.$global.ws.close()
      },
      onbeforeunload () {
        this.closeWebSocket()
      },

      searchGoods(){
        location.reload();
        this.$router.push({path:'/search',query:{goodName:this.search_word}})
      },
      store(){
        window.location.href = "http://localhost:1777/"
      },
      toHomepage () {
        this.$router.push('/')
      },
    },


  }
</script>

<style>
  .gs-link {
    color: #999;
    text-decoration: none;
    outline: none;
  }
  .gs-forbid {
    cursor: not-allowed;
  }
  .gs-menu-bg {
    position: absolute;

    width: 100%;
    height: 30px;
    background-color: #f2f2f2;
    border-bottom: 1px solid #e5e5e5;
  }
  .gs-header {
    width: 1120px;
    margin-left: auto;
    margin-right: auto;
  }
  .gs-quick-menu {
    position: relative;
    font-size: 12px;
    color: #999;
    background-color: #f2f2f2;
  }
  .gs-profile {
    display: inline-block;
    line-height: 30px;
    cursor: pointer;
  }
  .gs-nav {
    position: absolute;
    right: 0;
    display: inline-block;
    line-height: 30px;
    list-style: none;
  }
  .gs-nav-item {
    margin-left: 12px;
    display: inline-block;
  }
  .gs-logo {
    display: inline-block;
    margin-top: 10px;
    margin-bottom: 10px;
    width: 188px;
    height: 75px;
    cursor: pointer;
    background-image: url(./assets/images/pxx.jpg);
    background-repeat: no-repeat;
    background-size: cover;
  }
  .gs-search-box {
    display: inline-block;
    line-height: 100px;
    float: right;
  }
  .gs-search-input {
    box-sizing: border-box;
    width: 240px;
    line-height: 32px;
    padding-left: 6px;
    outline: none;
    border: 2px solid #00aaff;
    vertical-align: middle;
  }
  .gs-search-btn {
    display: inline-block;
    line-height: 36px;
    padding-left: 16px;
    padding-right: 16px;
    color: #fff;
    font-weight: 600;
    background-color: #00aaff;
    vertical-align: middle;
    cursor: pointer;
    user-select: none;
  }
  .gs-dividor {
    height: 2px;
    background-color: #3ab5ee;
  }
</style>

